package com.goktech.olala.core.service.impl;

import com.goktech.olala.core.resp.RespBuyInfoVo;
import com.goktech.olala.core.resp.RespOrderInfo;
import com.goktech.olala.core.service.IOrderDetailService;
import com.goktech.olala.server.dao.order.OrderDetailMapper;
import com.goktech.olala.server.dao.order.OrderMasterMapper;
import com.goktech.olala.server.pojo.order.OrderDetail;
import com.goktech.olala.server.pojo.order.OrderMaster;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Classname IOrderDetailServiceImp
 * @Description TODO
 * @Date 2020/12/26 14:47
 * @Created by ASUS
 */
@Service
public class IOrderDetailServiceImp implements IOrderDetailService {

    @Autowired
    OrderDetailMapper orderDetailMapper;
    @Autowired
    OrderMasterMapper orderMasterMapper;

    @Override
    public int deleteByPrimaryKey(Integer orderDetailId) {
        int i = orderDetailMapper.deleteByPrimaryKey(orderDetailId);
        return i;
    }

    @Override
    public int insert(OrderDetail record) {
        int insert = orderDetailMapper.insert(record);
        return insert;
    }

    @Override
    public OrderDetail selectByPrimaryKey(Integer orderDetailId) {
        OrderDetail orderDetail = orderDetailMapper.selectByPrimaryKey(orderDetailId);
        return orderDetail;
    }

    @Override
    public List<OrderDetail> selectAll() {
        List<OrderDetail> orderDetailList = orderDetailMapper.selectAll();
        return orderDetailList;
    }

    @Override
    public int updateByPrimaryKey(OrderDetail record) {
        int i = orderDetailMapper.updateByPrimaryKey(record);
        return i;
    }

    @Override
    public OrderDetail selectByOrderId(Integer orderId) {
        OrderDetail orderDetail = orderDetailMapper.selectByOrderId(orderId);
        return orderDetail;
    }

    //加入订单
    @Override
    public int addOrder(RespBuyInfoVo respBuyInfoVo) {
        //通过用户id找到订单id的集合
        String customerId = respBuyInfoVo.getCustomerId();
        String orderSn = respBuyInfoVo.getGoodSn();
        List<OrderMaster> orderIdList = orderMasterMapper.selectOrderId(customerId,orderSn);
        //有几个订单，放几个订单明细表
        for (int i = 0;i<orderIdList.size();i++){
            OrderDetail orderDetail = new OrderDetail();
            //订单表的订单id = 订单明细表的订单id
            orderDetail.setOrderId(orderIdList.get(i).getOrderId());
            orderDetail.setGoodsId(respBuyInfoVo.getGoodsId());
            orderDetail.setGoodsName(respBuyInfoVo.getGoodsName());
            orderDetail.setGoodsCnt(respBuyInfoVo.getGoodsAmount());
            orderDetail.setGoodsPrice(respBuyInfoVo.getGoodsPrice());
            orderDetail.setModifiedTime(new Date());

            int insert = orderDetailMapper.insert(orderDetail);
        }



        return 0;
    }

    @Override
    public List<RespOrderInfo> selectByCustomerId(String customerId, String goodSn) {
        //找到客户对应的订单id
        List<OrderMaster> orderIdList = orderMasterMapper.selectOrderId(customerId,goodSn);
        List<RespOrderInfo> orderDetailList=null;
        for (int i=0;i<orderIdList.size();i++){
            //通过订单id找订单
            Integer orderId = orderIdList.get(i).getOrderId();
            OrderDetail orderDetail = orderDetailMapper.selectByOrderId(orderId);
            //订单类出参
            RespOrderInfo respOrderInfo = new RespOrderInfo();
            respOrderInfo.setOrderSn(goodSn);
            respOrderInfo.setGoodsCnt(orderDetail.getGoodsCnt());
            respOrderInfo.setGoodsName(orderDetail.getGoodsName());
            respOrderInfo.setGoodsPrice(orderDetail.getGoodsPrice());
            respOrderInfo.setPayTime(orderDetail.getModifiedTime());
            respOrderInfo.setOrderMoney(orderDetail.getGoodsPrice()*orderDetail.getGoodsCnt());

            orderDetailList.add(respOrderInfo);
        }
        return orderDetailList;
    }


}
